package com.jtny.nytb.equipment.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.nytb.equipment.service.DeviceAreaService;
import com.jtny.nytb.equipment.utils.EasyExcelUtils;
import com.jtny.nytb.equipment.vo.typeAndArea.DeviceAreaExcelVO;
import com.jtny.nytb.equipment.vo.typeAndArea.DeviceAreaRequestVO;
import com.jtny.nytb.equipment.vo.typeAndArea.DeviceAreaResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(value = "设备区域", tags = "设备区域")
@ApiSort(202309)
@RequestMapping("/deviceArea")
public class DeviceAreaController {

    @Resource
    DeviceAreaService deviceAreaService;

    @ApiOperation(value = "保存设备区域")
    @PostMapping("/saveOrUpdateDevice")
    public WebResponse<?> saveOrUpdateDevice(@RequestBody List<DeviceAreaRequestVO> deviceAreaRequestVos) {
        deviceAreaService.saveOrUpdateDeviceArea(deviceAreaRequestVos);
        return WebResponse.success();
    }

    @ApiOperation(value = "启用禁用")
    @PostMapping("/enable")
    public WebResponse<?> enableDevice(@RequestBody DeviceAreaRequestVO deviceAreaRequestVo) {
        deviceAreaService.enableArea(deviceAreaRequestVo);
        return WebResponse.success();
    }

    @ApiOperation(value = "查询树")
    @GetMapping("/selectTree")
    public WebResponse<List<DeviceAreaResponseVO>> selectTree() {
        List<DeviceAreaResponseVO> deviceAreaResponseVos = deviceAreaService.selectTree();
        return WebResponse.success(deviceAreaResponseVos);
    }

    @ApiOperation(value = "查询list递归")
    @GetMapping("/selectListForRecu")
    public WebResponse<List<DeviceAreaResponseVO>> selectListForRecu(DeviceAreaRequestVO deviceAreaRequestVo) {
        List<DeviceAreaResponseVO> deviceAreaResponseVos = deviceAreaService.selectListForRecu(deviceAreaRequestVo);
        return WebResponse.success(deviceAreaResponseVos);
    }

    @ApiOperation(value = "查询list")
    @GetMapping("/selectList")
    public WebResponse<List<DeviceAreaResponseVO>> selectList(DeviceAreaRequestVO deviceAreaRequestVo) {
        List<DeviceAreaResponseVO> deviceAreaResponseVos = deviceAreaService.selectList(deviceAreaRequestVo);
        return WebResponse.success(deviceAreaResponseVos);
    }

    @ApiOperation(value = "分页查询list")
    @GetMapping("/selectPageList")
    public WebResponse<PageData<DeviceAreaResponseVO>> selectPageList(DeviceAreaRequestVO deviceAreaRequestVo) {
        PageData<DeviceAreaResponseVO> deviceAreaResultVos = deviceAreaService.selectPageList(deviceAreaRequestVo);
        return WebResponse.success(deviceAreaResultVos);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/remove/{ids}")
    public WebResponse<?> removeByIds(@PathVariable List<Long> ids) {
        deviceAreaService.removeByIds(ids);
        return WebResponse.success();
    }

    @PostMapping("/exportTemp")
    @ApiOperation(value = "导出模板")
    public void exportTemp(HttpServletResponse response) {
        EasyExcelUtils.exportExcel(response, "设备区域", null, DeviceAreaExcelVO.class);
    }

    @PostMapping("/importExcel")
    @ApiOperation(value = "导入")
    public WebResponse<?> importExcel(@RequestParam MultipartFile file) {
        List<DeviceAreaExcelVO> objects =
                (List<DeviceAreaExcelVO>) EasyExcelUtils.importExcel(file, DeviceAreaExcelVO.class);
        deviceAreaService.exportExcel(objects);
        return WebResponse.success();
    }
}
